<template>
	<view class="content">
		<uni-card class="card" v-for="opinion in opinions" :key="opinion.id">
			<textarea class="text">{{opinion.content}}
			</textarea>
			<view class="jiazhang">
				反馈家长：{{opinion.patriarchName}}
			</view>
		</uni-card>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				opinions:[],
			}
		},
		methods: {
			getJwtToken() {
				try {
					return wx.getStorageSync('jwt_token');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			sendRequestWithToken(url, method) {
				const token = this.getJwtToken();
				if (token) {
					wx.request({
						url: url,
						method: method,
						header: {
							'token': token
						},
						success: (res) => {
							if (res.data.code === 1 && res.data.msg === 'success') {
								this.opinions = res.data.data;
							} else if (res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: null
								});
								this.goPage(7);
								console.error('未登录', res.data.data);
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '网络请求失败',
								icon: 'none'
							});
							console.error('请求失败', error);
						}
					});
				} else {
					this.goPage(7);
					// 如果没有令牌，处理没有令牌的情况，比如跳转到登录页  
					console.error('No token found, please login first.');
				}
			},
			getuserName() {
				try {
					return wx.getStorageSync('user_name');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			showOpinion() {
				const username = this.getuserName();
				if (username) {
					const url = `http://localhost:8080/teacher/opinion/${username}`;
					const method = 'GET';
					const data = this.opinion;
					this.sendRequestWithToken(url, method)
					}
					else {
						console.error('用户名未找到');
					}
				},
				onLoad() {
					this.showOpinion();
				}

			}
		}
</script>

<style>
	.content {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.card {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 20rpx;
		height: 500rpx;
		width: 90%;
		background-color: #F3F3F3;
	}
	.text{
		font-size: 30rpx;
		font-family: AliMamaShuHei;
		font-weight: bold;
	}
	.jiazhang{
		font-size: 25rpx;
		font-family: AliMamaShuHei;
		font-weight: bold;
	}

</style>